#include "drawjunctionitem.h"


DrawJunciontItem::DrawJunciontItem(QObject *parent): QObject(parent)
{
    setFlag(QGraphicsPathItem::ItemIsSelectable);
    setAcceptHoverEvents(true);
//    setAcceptTouchEvents(true);
    m_iStart = 0;
}

void DrawJunciontItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter->setBrush(QColor(255, 255, 0, 100));
    painter->setPen(QPen(Qt::red, 0.1));

    qDebug() << "DrawJunciontItem Begin Paint";

     //关键在合理调整连接点算法
    QPolygonF polygonAll = drawJuncionAlogrithum(painter);

    qDebug() << "DrawJunciontItem End Paint";

    this->setPolygon(polygonAll);
}

QPolygonF DrawJunciontItem::drawJuncionAlogrithum(QPainter *painter)
{
    QPointF qPointA1 = m_qRecFFirst.topRight();
    QPointF qPointA2 = m_qRecFFirst.bottomRight();

    QPointF qPointB1 = m_qRecFSecond.topLeft();
    QPointF qPointB2 = m_qRecFSecond.bottomLeft();

    QPointF qPointC1 = m_qRecFThird.bottomLeft();
    QPointF qPointC2 = m_qRecFThird.bottomRight();

    QPainterPath qPainterPath1;
    QPainterPath qPainterPath2;


    QPolygonF polygonLink1;
    polygonLink1 << qPointA1 << qPointB1 << qPointB2 << qPointA2;
    qPainterPath1.addPolygon(polygonLink1);

    QPolygonF polygonLink2;
    polygonLink2 << qPointA1 << qPointC1 << qPointC2 << qPointA2;
    qPainterPath2.addPolygon(polygonLink2);

    QPolygonF polygonAll;
    polygonAll << qPointA1 << qPointB1 << qPointB2 << qPointA2 << qPointA1 << qPointC1 << qPointC2 << qPointA2;

    painter->drawPath(qPainterPath1);
    painter->drawPath(qPainterPath2);

    return polygonAll;
}

void DrawJunciontItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{

}

void DrawJunciontItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{

}

void DrawJunciontItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{

}

void DrawJunciontItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
    qDebug() << "DrawJunciont hoveEvent, Pos: " << event->pos();
}

void DrawJunciontItem::setRect(QRectF qRectf)
{
    if(0 == m_iStart)
    {
        m_iStart = 1;
        m_qRecFFirst = qRectf;
    }
    else if(1 == m_iStart)
    {
        m_iStart = 2;
        m_qRecFSecond = qRectf;
    }
    else
    {
        m_qRecFThird = qRectf;
    }
}
